package org.msfj.admin.data.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.admin.data.service.DictService;
import org.msfj.admin.model.Dict;
import org.msfj.admin.model.DictIndex;
import org.msfj.admin.vo.DictVo;
import org.msfj.admin.data.mapper.DictIndexMapper;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 系统数据字典明细信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "dict")
public class DictServiceImpl extends BaseServiceImpl<Dict> implements DictService {


    @Autowired
    DictIndexMapper dictIndexMapper;

    /**
     * 根据条件分页查询系统数据字典明细信息列表
     *
     * @param dicVo
     * @param page  当前第几页
     * @param rows  每页多少条记录
     * @return PageInfo<Dict>
     */
    @Override
    //@Cacheable
    public PageInfo<DictVo> getByDic(DictVo dicVo, int page, int rows) {
        Example example = new Example(Dict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        PageHelper.startPage(page, rows);
        List<Dict> dictList = super.getByExample(example);
        PageInfo dicVoPageInfo = new PageInfo(dictList);
        dicVoPageInfo.setList(setOtherProperties(dictList));
        return dicVoPageInfo;
    }

    /**
     * 根据条件查询所有系统数据字典明细信息对象列表
     *
     * @param dicVo
     * @return List<Dict>
     */
    @Override
    //@Cacheable
    public List<DictVo> getAllDic(DictVo dicVo) {
        Example example = new Example(Dict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode()).andEqualTo("indexId", dicVo.getIndexId());
        example.setOrderByClause("sort_no asc");
        List<DictVo> dicVoList = setOtherProperties(super.getByExample(example));
        return dicVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param dict
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public DictVo setOtherProperties(Dict dict) {
        DictVo dicVo = new DictVo();
        BeanUtils.copyProperties(dict, dicVo);
        return dicVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param dicts
     * @return List<DictVo>
     */
    @Override
    //@Cacheable
    public List<DictVo> setOtherProperties(List<Dict> dicts) {
        List<DictVo> dicVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dicts)) {
            for (Dict dict : dicts) {
                DictVo dicVo = new DictVo();
                BeanUtils.copyProperties(dict, dicVo);
                dicVos.add(dicVo);
            }
        }
        return dicVos;
    }

    /**
     * 根据code查询codeText
     *
     * @param code
     * @return
     */
    @Override
    public DictVo getByCode(String code) {
        Example example = new Example(Dict.class);
        example.createCriteria().andEqualTo("enable", Enable.ENABLE.getCode()).andEqualTo("code", code);
        List<Dict> byExample = super.getByExample(example);
        if (CollectionUtil.isNotEmpty(byExample)) {
            return setOtherProperties(byExample.get(0));
        }
        return null;
    }

    @Override
    public Map<String, String> getAllTextByIndexKey(String indexKey) {
        Map<String, String> map = new HashMap<>();
        DictIndex dictIndex = new DictIndex();
        dictIndex.setKey(indexKey);
        dictIndex = dictIndexMapper.selectOne(dictIndex);
        if (dictIndex != null) {
            Example example = new Example(Dict.class);
            example.createCriteria().andEqualTo("enable", Enable.ENABLE.getCode()).andEqualTo("indexId", dictIndex.getId());
            List<Dict> byExample = super.getByExample(example);
            if (byExample != null) {
                for (Dict dict : byExample) {
                    map.put(dict.getCode(), dict.getCodeText());
                }
            }
        }
        return map;
    }
}
